from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Edge()
driver.get("https://www.baidu.com")  # 打开网页

print(driver.title)  # 获取页面的标题
print(driver.current_url)  # 获取当前页面的 URL
# 显示等待
element = WebDriverWait(driver, 15).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, "#s-top-left > a:nth-child(1)"))
)
print(driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(1)").text)  # 获取元素的文本内容

sleep(2)

input_box = driver.find_element(By.ID, "kw")  # 查找元素

sleep(2)

input_box.send_keys("迪丽热巴")  # 在输入框中键入内容

sleep(2)

search_button = driver.find_element(By.ID, "su")

sleep(2)

print(search_button.get_attribute("value"))  # 获取元素value属性

sleep(2)

search_button.click()  # 点击元素

sleep(2)

input_box.clear()  # 清空输入框

sleep(2)

input_box.send_keys("杨幂")

sleep(2)

search_button.click()

sleep(2)

driver.back()  # 页面后退

sleep(2)

driver.forward()  # 页面前进

sleep(2)

driver.refresh()  # 页面的刷新

sleep(2)

driver.maximize_window()  # 最大化窗口

sleep(2)

driver.set_window_size(500, 500)  # 设置窗口尺寸

sleep(2)

driver.quit()  # 浏览器关闭
